package com.boot.project.service.impl;

import com.boot.project.dao.UserDao;
import com.boot.project.dao.UserInfoDao;
import com.boot.project.domain.UserDO;
import com.boot.project.domain.UserInfoDo;
import com.boot.project.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserInfoDao userInfoDao;

    @Override
    public UserDO selectByOpenId(String open_id) {
//        return null;
        return userDao.selectByOpenId(open_id);
    }

    @Override
    public int updateById(UserDO userDO) {
        return userDao.updateById(userDO);
    }

    @Override
    public Boolean insert(UserDO userDO) {
        return userDao.insert(userDO);
    }

    @Override
    public UserInfoDo
    userInfo(String openId) {
        return userInfoDao.userInfo(openId);
    }

    @Override
    public Map<String, Object> updateUserInfo(UserInfoDo userInfoDo) {
        Map<String, Object> mapResult = new HashMap<>();
        // 先看看这个用户是修改还是新增
        if (userInfoDao.userInfo(userInfoDo.getOpenId()) == null){
            // 如果为空，则新增
            Integer saveFlag = userInfoDao.saveInfo(userInfoDo);
            if (saveFlag >= 0) {
                mapResult.put("msg", "ok");
                mapResult.put("code", 200);
                return mapResult;
            } else {
                mapResult.put("msg", "error");
                mapResult.put("code", 500);
                return mapResult;
            }

        }else {
            // 则代表表中已有该用户数据，此时为更新
            Integer updateFlag = userInfoDao.updateInfo(userInfoDo);
            if (updateFlag >= 0) {
                mapResult.put("msg", "ok");
                mapResult.put("code", 200);
                return mapResult;
            } else {
                mapResult.put("msg", "error");
                mapResult.put("code", 500);
                return mapResult;
            }
        }
    }

    @Override
    public Long getIdByOpenId(String openId) {
        return userDao.getIdByOpenId(openId);
    }

    @Override
    public Map<String, Object> updateIntegral(Long userId, Double integral) {
        Map<String, Object> mapResult = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("integral", integral);
        Integer updateFlag = userDao.updateIntegral(params);
        if (updateFlag >= 0) {
            mapResult.put("msg", "ok");
            mapResult.put("code", 200);
            return mapResult;
        } else {
            mapResult.put("msg", "error");
            mapResult.put("code", 500);
            return mapResult;
        }
    }

    @Override
    public List<UserDO> allUsers() {
        return userDao.allUsers();
    }

    @Override
    public Integer getWorkUser(String openId) {
        return userDao.getWorkUser(openId);
    }

    @Override
    public UserDO userBalanceAndIntegral(String openId) {
        return userDao.userBalanceAndIntegral(openId);
    }

    @Override
    public Map<String, Object> saveMobile(Long mobile, String openId) {
        Map<String, Object> mapResult = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        params.put("mobile", mobile);
        params.put("openId", openId);
        Integer saveFlag = userDao.saveMobile(params);
        if (saveFlag >= 0) {
            mapResult.put("msg", "ok");
            mapResult.put("code", 200);
            return mapResult;
        } else {
            mapResult.put("msg", "error");
            mapResult.put("code", 500);
            return mapResult;
        }
    }

}
